package com.cinema.dao.impl;

import com.cinema.dao.ScreeningRoomDao;
import com.cinema.db.DatabaseManager;
import com.cinema.entity.ScreeningRoom;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 放映厅数据访问实现类
 */
public class ScreeningRoomDaoImpl implements ScreeningRoomDao {
    
    private final DatabaseManager dbManager = DatabaseManager.getInstance();
    
    @Override
    public void addRoom(ScreeningRoom room) {
        String sql = "INSERT INTO screening_rooms (id, name, rows, cols) VALUES (?, ?, ?, ?)";
        try {
            dbManager.executeUpdate(sql, room.getId(), room.getName(), room.getRows(), room.getCols());
        } catch (SQLException e) {
            throw new RuntimeException("添加放映厅失败", e);
        }
    }
    
    @Override
    public ScreeningRoom getRoomById(String id) {
        String sql = "SELECT id, name, rows, cols FROM screening_rooms WHERE id = ?";
        try (ResultSet rs = dbManager.executeQuery(sql, id)) {
            if (rs.next()) {
                return mapResultSetToRoom(rs);
            }
            return null;
        } catch (SQLException e) {
            throw new RuntimeException("查询放映厅失败", e);
        }
    }
    
    @Override
    public List<ScreeningRoom> getAllRooms() {
        String sql = "SELECT id, name, rows, cols FROM screening_rooms ORDER BY id";
        List<ScreeningRoom> rooms = new ArrayList<>();
        try (ResultSet rs = dbManager.executeQuery(sql)) {
            while (rs.next()) {
                rooms.add(mapResultSetToRoom(rs));
            }
            return rooms;
        } catch (SQLException e) {
            throw new RuntimeException("查询所有放映厅失败", e);
        }
    }
    
    @Override
    public void updateRoom(ScreeningRoom room) {
        String sql = "UPDATE screening_rooms SET name = ?, rows = ?, cols = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?";
        try {
            int rows = dbManager.executeUpdate(sql, room.getName(), room.getRows(), room.getCols(), room.getId());
            if (rows == 0) {
                throw new RuntimeException("放映厅不存在: " + room.getId());
            }
        } catch (SQLException e) {
            throw new RuntimeException("更新放映厅失败", e);
        }
    }
    
    @Override
    public void deleteRoom(String id) {
        String sql = "DELETE FROM screening_rooms WHERE id = ?";
        try {
            int rows = dbManager.executeUpdate(sql, id);
            if (rows == 0) {
                throw new RuntimeException("放映厅不存在: " + id);
            }
        } catch (SQLException e) {
            throw new RuntimeException("删除放映厅失败", e);
        }
    }
    
    @Override
    public boolean roomExists(String id) {
        String sql = "SELECT COUNT(*) FROM screening_rooms WHERE id = ?";
        try (ResultSet rs = dbManager.executeQuery(sql, id)) {
            if (rs.next()) {
                return rs.getInt(1) > 0;
            }
            return false;
        } catch (SQLException e) {
            throw new RuntimeException("检查放映厅是否存在失败", e);
        }
    }
    
    private ScreeningRoom mapResultSetToRoom(ResultSet rs) throws SQLException {
        String id = rs.getString("id");
        String name = rs.getString("name");
        int rows = rs.getInt("rows");
        int cols = rs.getInt("cols");
        
        return new ScreeningRoom(id, name, rows, cols);
    }
} 